<?php

/**
 * setup actions.
 *
 * @package    s-test
 * @subpackage setup
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class setupActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {

  }
  public function executeSetup(sfWebRequest $request){
    $dir = getcwd();
 
    chdir(sfConfig::get('sf_root_dir'));
    
    $this->hasErrors = false;
    //On va charger créer les tables SQL
    try{
        $create_task = new sfDoctrineBuildTask($this->dispatcher, new sfFormatter());
        $create_task->run(array(), array('all'));
    }catch(Exception $e){

       chdir($dir);
       $this->hasErrors = true;
       $this->error_title = "Build all";
       $this->error = $e->getMessage();

       return $this->renderText($this->error_title.'<br/>'.$this->error);
    }
    try{
        $insert_sql_task = new sfDoctrineInsertSqlTask($this->dispatcher, new sfFormatter());
        $insert_sql_task->run();
    }
    catch(Exception $e){
       $this->hasErrors = true;
       $this->error_title = "Create Tables";
       $this->error = $e->getMessage();

       return $this->renderText($this->error_title.'<br/>'.$this->error);
    }
    //Puis charger les infos de base...
    try{
        $load_task = new sfDoctrineDataLoadTask($this->dispatcher, new sfFormatter());
        $load_task->run();
    }catch(Exception $e){
        chdir($dir);

        $this->hasErrors = true;
        $this->error_title = "Data Load";
        $this->error = $e->getMessage();
        
        return $this->renderText($this->error_title.'<br/>'.$this->error);
        
    }

    $this->redirect("@homepage");
  }
}
